package com.mark.controller.common;

import com.mark.common.Const;
import com.mark.pojo.Admin;
import com.mark.pojo.User;
import com.mark.util.*;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Mark on 2018/4/21.
 * 通过过滤器配置：重置单点登录session有效期
 */
public class SessionExpireFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String userOrigin = PropertiesUtil.getProperty("user.origin");
        String adminOrigin = PropertiesUtil.getProperty("admin.origin");
        String Access_Control_Allow_Origin_value = "";
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String requestOrigin = httpServletRequest.getHeader("Origin");
        if(StringUtils.equals(requestOrigin,userOrigin)){
            Access_Control_Allow_Origin_value = requestOrigin;
            String loginToken = CookieUtil.readLoginToken(httpServletRequest);
            if(StringUtils.isNotEmpty(loginToken)){//拿到的loginToken不为空
                String userJsonStr = RedisShardedPoolUtil.get(loginToken);
                User currentUser = JsonUtil.string2Obj(userJsonStr,User.class);
                if(currentUser != null){//字符串装换为对象不为空
                    RedisShardedPoolUtil.expire(loginToken, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);//重置时间
                }
            }
        }else if(StringUtils.equals(requestOrigin,adminOrigin)){
            Access_Control_Allow_Origin_value = requestOrigin;
            String loginToken = CookieUtil.readAdminLoginToken(httpServletRequest);
            if(StringUtils.isNotEmpty(loginToken)){//拿到的loginToken不为空
                String adminJsonStr = RedisShardedPoolUtil.get(loginToken);
                Admin currentAdmin = JsonUtil.string2Obj(adminJsonStr,Admin.class);
                if(currentAdmin != null){//字符串装换为对象不为空
                    RedisShardedPoolUtil.expire(loginToken, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);//重置时间
                }
            }
        }

        addHeader(response,Access_Control_Allow_Origin_value);
        chain.doFilter(request,response);
    }


    public void addHeader(ServletResponse res,String value) {
        HttpServletResponse response = (HttpServletResponse)res;
        response.setHeader("Access-Control-Allow-Origin", value); // 允许指定域访问跨域资源
        //设置允许的跨域，因为在ngnix配置了这里先注解
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,X-File-Name");
        response.setHeader("Access-Control-Allow-Credentials", "true"); // 允许客户端携带跨域cookie，此时origin值不能为“*”，只能为指定单一域名
    }

    @Override
    public void destroy() {

    }
}
